<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inscription extends CI_Controller
{   
    public function __construct()
    {
        parent::__construct();
		
		//On charge le modèle user
		$this->load->model("user",'newUser');
		$this->load->helper('url');
		$this->load->config('facebook');
		
		$fb_config['appId'] 	= $this->config->item('appId'); 
		$fb_config['secret'] 	= $this->config->item('secret'); 

        $this->load->library('facebook', $fb_config);
    }
    
    public function index()
	{
		$this->inscription();
	}
	
	public function inscription()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		 
		$this->form_validation->set_rules('name'		,'"Nom"'		,	'trim|required|min_length[1]|max_length[255]|alpha_dash|encode_php_tags|xss_clean');
		$this->form_validation->set_rules('firstname'	,'"Prénom"'		,	'trim|required|min_length[1]|max_length[255]|alpha_dash|encode_php_tags|xss_clean');
		$this->form_validation->set_rules('email'		,'"Email"'		,	'trim|required|min_length[3]|max_length[255]|encode_php_tags|xss_clean');
		$this->form_validation->set_rules('pass'		, 'Mot de passe', 	'trim|required|min_length[6]|matches[confirm]');
		$this->form_validation->set_rules('confirm'		, 'Confirmation', 	'trim|required|');
		
		if ($this->form_validation->run() == FALSE)
			$this->connectWithFacebook();
		else
		{
			//On initialise l'utilisateur
			$userId = $this->newUser->create(	$this->input->post('name'),
									$this->input->post('firstname'),
									$this->input->post('email'),
									$this->input->post('gender'),
									$this->input->post('pass'),
									NULL);
								
			if($userId != false)
			{
				$this->session->set_userdata('user'	, $this->newUser->getUserById($userId));
				
				$this->load->template('accueil');
			}
			else
				redirect('/inscription/inscription', 'refresh');
		}
	}
	
	public function connectWithFacebook()
	{	
		$user = $this->facebook->getUser();
		
		$user_profile = $this->authenticate_user($user);
		
		// Si l'utilisateur n'est pas authentifié on récupère l'url de login
		if (!$user)
			$loginUrl 	= $this->facebook->getLoginUrl(array('scope' => 'email'));
		
		$data = array(
		'user' 			=> 	$user,
		'loginUrl' 		=> 	isset($loginUrl) ? $loginUrl : NULL,
		'pb_email'		=>	$this->uri->segment(2),
		'session_user'	=>	$this->session->userdata('user'));
		
		
			$this->load->template('inscription', $data);
	}
	
	/** Fonction permetttant d'authentifier un utilisateur facebook*/
	private function authenticate_user($user)
	{
		if ($user) {
			try 
			{
				$profile = $this->facebook->api('/me');
					
				//On initialise l'utilisateur
				$userId = $this->newUser->create($profile['last_name'],
										$profile['first_name'],
										$profile['email'],
										$profile['gender'] == "male" ? 1 : 2,
										NULL,
										'https://graph.facebook.com/'.$user.'/picture');
										
				// On envoi l'utilisateur dans la variable de session
				$this->session->set_userdata('user'	, $this->newUser->getUserById($userId));
				
				
				return $profile;
				
			} catch (FacebookApiException $e) 
			{
				error_log($e);
			}
		}
		return FALSE;
	}
	
	public function logout()
	{
		$this->facebook->destroySession();
		$this->session->unset_userdata('user');
		redirect('', 'location');
	}
}

/* End of file inscription.php */
/* Location: ./application/controllers/inscription.php */